<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="是否支付" prop="isPayed">-->
<!--        <el-select v-model="queryParams.isPayed" placeholder="请选择是否支付" clearable size="small">-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.bu_is_no"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          ></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="用户" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="咨询师" prop="zxsname">
        <el-input
          v-model="queryParams.zxsname"
          placeholder="请输入咨询师"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['business:order:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['business:order:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:order:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="全部" name=""></el-tab-pane>
      <el-tab-pane label="未付款" name="1"></el-tab-pane>
      <el-tab-pane label="待咨询" name="2"></el-tab-pane>
<!--      <el-tab-pane label="待评价" name="9"></el-tab-pane>-->
      <el-tab-pane label="已完成" name="3"></el-tab-pane>
    </el-tabs>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="订单编号" align="center" prop="orderNo" />
      <el-table-column label="用户" align="center" prop="username" />
      <el-table-column label="咨询师" align="center" prop="zxsname" />
      <el-table-column label="实付金额" align="center" prop="totalPrice" />
      <el-table-column label="订单状态" align="center" prop="orderStatus" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="orderTime" width="180"/>

      <el-table-column label="支付时间" align="center" prop="payTime" width="180" />
      <el-table-column label="完成时间" align="center" prop="finallyTime" width="180" />
<!--      <el-table-column label="取消时间" align="center" prop="cancelTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.cancelTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="咨询开始时间" align="center" prop="startDate" width="180" />
      <el-table-column label="咨询结束时间" align="center" prop="endDate" width="180" />


<!--      <el-table-column label="是否支付" align="center" prop="isPayed" >-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.bu_is_no" :value="scope.row.isPayed"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="退款状态" align="center" prop="refundStatus" >-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.refund_status" :value="scope.row.refundStatus"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="订单关闭原因 1-超时未支付 2-退款关闭 4-买家取消 15-已通过货到付款交易" align="center" prop="closeType" />-->
<!--      <el-table-column label="订单标题" align="center" prop="title" />-->
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作"  fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:order:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="订单编号" prop="orderNo">
          <el-input v-model="form.orderNo" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="状态" prop="orderStatus">
          <el-select v-model="form.orderStatus" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.order_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户" />
        </el-form-item>
        <el-form-item label="咨询师" prop="zxsname">
          <el-input v-model="form.zxsname" placeholder="请输入咨询师" />
        </el-form-item>
        <el-form-item label="实付金额" prop="totalPrice">
          <el-input v-model="form.totalPrice" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="下单时间" prop="orderTime">
          <el-date-picker clearable size="small"
            v-model="form.orderTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择下单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="完成时间" prop="finallyTime">
          <el-date-picker clearable size="small"
            v-model="form.finallyTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择完成时间">
          </el-date-picker>
        </el-form-item>
<!--        <el-form-item label="取消时间" prop="cancelTime">-->
<!--          <el-date-picker clearable size="small"-->
<!--            v-model="form.cancelTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="选择取消时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
        <el-form-item label="咨询开始时间" prop="startDate">
          <el-date-picker clearable size="small"
            v-model="form.startDate"
             type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择咨询开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="咨询结束时间" prop="endDate">
          <el-date-picker clearable size="small"
            v-model="form.endDate"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择咨询结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable size="small"
            v-model="form.payTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择支付时间">
          </el-date-picker>
        </el-form-item>
<!--        <el-form-item label="是否已经支付，0：已经支付过   1：，没有支付过" prop="isPayed">-->
<!--          <el-input v-model="form.isPayed" placeholder="请输入是否已经支付，0：已经支付过   1：，没有支付过" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="订单标题" prop="title">-->
<!--          <el-input v-model="form.title" placeholder="请输入订单标题" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" placeholder="请输入备注" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="${comment}" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
<!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder } from "@/api/business/order";

export default {
  name: "Order",
  dicts: ['bu_is_no','refund_status','order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: '',
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        userId: null,
        zxsId: null,
        totalPrice: null,
        orderStatus: null,
        orderTime: null,
        finallyTime: null,
        cancelTime: null,
        startDate: null,
        endDate: null,
        payTime: null,
        isPayed: null,
        refundStatus: null,
        closeType: null,
        title: null,
        username: null,
        zxsname: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleClick(tab, event) {
      this.queryParams.orderStatus = tab.name
      this.getList()
    },
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNo: null,
        userId: null,
        zxsId: null,
        totalPrice: null,
        orderStatus: "0",
        orderTime: null,
        finallyTime: null,
        cancelTime: null,
        startDate: null,
        endDate: null,
        payTime: null,
        isPayed: null,
        refundStatus: "0",
        closeType: null,
        title: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
